﻿using UnityEngine;
using UnityEngine.UI;

namespace Example_0000_Atlas_02
{
	public class UIMain : MonoBehaviour {

		AssetBundle assetbundle = null;
		void Start () 
		{
			CreatImage(loadSprite("L pose-2",GetAssetBundle("Atlas2")));//assetbundle：Atlas2 ，图集：Atlas2
			CreatImage(loadSprite("L pose-1",GetAssetBundle("Atlas2")));
		}
	
		private void CreatImage(Sprite sprite ){
			GameObject go = new GameObject(sprite.name);
			go.layer = LayerMask.NameToLayer("UI");
			go.transform.parent = transform;
			go.transform.localScale= Vector3.one;
			Image image = go.AddComponent<Image>();
			image.sprite = sprite;
			image.SetNativeSize();
		}
		private string GetAssetBundle(string bundlename){
			return string.Format("{0}/Example_0000_Atlas_02/{1}.assetbundle",Application.streamingAssetsPath,bundlename) ;
		}
	
		private Sprite loadSprite(string spriteName,string bundlePath){
	//#if USE_ASSETBUNDLE
			if(assetbundle == null)
				// assetbundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath +"/Example_0000_Atlas_02/Atlas2.assetbundle");//已过时
				// 	return assetbundle.Load(spriteName) as Sprite;//已过时

				assetbundle = AssetBundle.LoadFromFile(bundlePath);
				return assetbundle.LoadAsset(spriteName) as Sprite;
	// #else
	// 		return Resources.Load<GameObject>("Example_0000_Atlas_01/" + spriteName).GetComponent<SpriteRenderer>().sprite;
	// #endif	
		}
	}
}

